﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

public class AttackInfoScript : MonoBehaviour {

    Animator animator;
    BoxCollider2D boxColl;

    public float decayTime;
    public float damage;
    private float speed;

    public bool moveAttack1;

    // Use this for initialization
    void Start ()
    {
        moveAttack1 = false;
        animator = GetComponent<Animator>();
        boxColl = GetComponent<BoxCollider2D>();

        if (gameObject.transform.tag == "Attack1")
        {
            decayTime = 1.5f;
            damage = 2;
            speed = 0.1f;
        }
        else if(gameObject.transform.tag == "Attack2")
        {
            decayTime = 2f;
            damage = 4;
        }
        else if (gameObject.transform.tag == "Attack3")
        {
            decayTime = 5f;
            damage = 5;

            float distanceRight = Vector3.Distance(GameObject.Find("AttackSpawnPointRight").transform.position, GameObject.Find("RightBorder").transform.position);
            float distanceLeft = Vector3.Distance(GameObject.Find("AttackSpawnPointLeft").transform.position, GameObject.Find("LeftBorder").transform.position);
            if (distanceRight <= distanceLeft)
            {
                GetComponent<SpriteRenderer>().flipX = true;
            }

        }
        else if (gameObject.transform.tag == "Attack4")
        {
            decayTime = 4;
            damage = 9;
        }

        StartCoroutine(WaitForTimer(decayTime));
    }

    void FixedUpdate()
    {
        if (moveAttack1 == true)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y + speed, transform.position.z);
        }
        else
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        }

        if (gameObject.transform.tag == "Attack2" && boxColl.enabled == true)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y + 0.0001f, transform.position.z);
        }

        if (gameObject.transform.tag == "Attack3" && boxColl.enabled == true)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y + 0.0001f, transform.position.z);
        }

        if (gameObject.transform.tag == "Attack4")
        {
            transform.position = new Vector3(transform.position.x + 0.025f, transform.position.y, transform.position.z);
        }
    }

    IEnumerator WaitForTimer(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        animator.SetBool("Attack1Hit", true);
        Die();
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "Enemy")
        {
            col.gameObject.GetComponent<EnemyControllScript>().enemyHealth -= damage;
            animator.SetBool("Attack1Hit", true);
        }
    }

    public void ActiviateBoxColl()
    {
        boxColl.enabled = true;
    }

    public void StartMoving()
    {
        moveAttack1 = true;
    }

    public void StopMoving()
    {
        moveAttack1 = false;
    }

    public void Die()
    {
        Destroy(gameObject);
    }
}
